<?php
/**
 * 专营店营销基金线上传播模型
 * @author Administrator
 *
 */
class commonOnlineModel extends MY_Model{
    function commonOnlineModel(){
        parent::MY_Model();
        $this->table = 'CommonOnline';
    }
    /**
     * 更新父级金额
     * {@inheritDoc}
     * @see MY_Model::updateParentMoneys()
     */
    function updateParentMoneys($parentId){
        $this->load->model('commonProcessModel');
        $this->commonProcessModel->updateMoney($parentId);
    }
    /**
     * 获取父级金额ID
     * {@inheritDoc}
     * @see MY_Model::getParentId()
     */
    function getParentId($data = array()){
        return isset($data['procId']) && $data['procId'] ? (int)$data['procId'] : 0;
    }
    /**
     * 包装数据
     * {@inheritDoc}
     * @see MY_Model::packing()
     */
    function packing($list = array()){
        //活动类型配置
        $this->load->model('carLevelModel');
        $carLevelList = $this->carLevelModel->getCacheData('carLevelList');
        
        //$mediaType = $this->config->item('onlineMediaType');
        //角色列表
        $roleList = $this->config->item('roleList');
        //审核结果
        $auditRs = $this->config->item('auditRs');
        foreach($list as $k => $v){
            //反序列化凭证数据
            $v['voucher'] = unserialize($v['voucher']);
            //活动类型
            /* $v['mediaTypeStr'] = isset($mediaType[$v['mediaType']]) ? $mediaType[$v['mediaType']] : '';
            $v['mediaType'] == -1 && $v['mediaTypeStr'] .= '-'.$v['mediaTypeAlias']; */
            $v['mediaTypeStr'] = isset($carLevelList[$v['mediaType']]) ? $carLevelList[$v['mediaType']]['name'] : '';
            //预算审核状态
            $v['budgetAuditStr'] = isset($roleList[$v['budgetAuditRole']]) ? $roleList[$v['budgetAuditRole']].'已审核' : '';
            if ($v['budgetAuditStr']){
                $v['budgetAuditStr'] .= isset($auditRs[$v['budgetAuditRs']]) ? ','.$auditRs[$v['budgetAuditRs']] : '';
            }else{
                $v['budgetAuditStr'] = isset($auditRs[$v['budgetAuditRs']]) ? $auditRs[$v['budgetAuditRs']] : '';
            }
            //结算审核状态
            $v['settleAuditStr'] = isset($roleList[$v['settleAuditRole']]) ? $roleList[$v['settleAuditRole']].'已审核' : '';
            if ($v['settleAuditStr']){
                $v['settleAuditStr'] .= isset($auditRs[$v['settleAuditRs']]) ? ','.$auditRs[$v['settleAuditRs']] : '';
            }else{
                $v['settleAuditStr'] = isset($auditRs[$v['settleAuditRs']]) ? $auditRs[$v['settleAuditRs']] : '';
            }
            
            $list[$k] = $v;
        }
        return $list;
    }
    /**
     * 更新金额
     * @param unknown $onlineId
     */
    function updateMoney($onlineId){
        $findData = $this->getData($onlineId);
        
        $data = array();
        $data['lastSettleAudit'] = min((float)$findData['budgetAudit'], (float)$findData['settleAudit']*$findData['kpi']);
        $this->edit($onlineId, $data);
    }
    /**
     * 统计对应父级ID的金额
     * @param unknown $procId
     */
    function getMoney($procId){
        $data = array(
            'budgetApply' => 0,
            'budgetAudit' => 0,
            'settleApply' => 0,
            'settleAudit' => 0,
        );
        $findList = $this->getList(array('procId'=>$procId));
        foreach ($findList as $v){
            $data['budgetApply'] += $v['budgetApply'];
            in_array($v['budgetAuditRs'], array(1,4)) && $data['budgetAudit'] += $v['budgetAudit'];
            $data['settleApply'] += $v['settleApply'];
            //最终结算审核金额
            in_array($v['settleAuditRs'], array(1,4)) && $data['settleAudit'] += $v['lastSettleAudit'];
        }
        return $data;
    }
    /**
     * 预算审核状态统计
     * @param int $procId
     * @return multitype:
     */
    function getBudgetStatusCount($procId) {
        $findList = $this->getList(array('procId'=>$procId,'budgetApply > '=>0), null, null, 'budgetAuditRs');
        $count = array();
        foreach ($findList as $v){
            $count[$v['budgetAuditRs']] ++;
        }
        return $count;
    }
    /**
     * 单个预算审核
     * @param number $id
     * @param number $audit
     * @param array $data
     * @return object
     */
    function budgetAudit($id, $audit = 1, $user = array()){
        $findData = $this->getData($id);
        $auditData = array(
            'budgetAudit' =>  $audit==1 ? $findData['budgetApply'] : 0,
            'budgetAuditRs' => $audit,
            'budgetAuditRole' => $user['userRole']
        );
        return $this->db->update($this->table, $auditData,array('id'=>$id));
    }
    /**
     * 结算审核状态统计
     * @param int $procId
     * @return multitype:
     */
    function getSettleStatusCount($procId) {
        $findList = $this->getList(array('procId'=>$procId,'settleApply > '=>0), null, null, 'settleAuditRs');
        $count = array();
        foreach ($findList as $v){
            $count[$v['settleAuditRs']] ++;
        }
        return $count;
    }
    /**
     * 结算单个审核
     * @param number $id
     * @param number $audit
     * @param array $user
     * @return object
     */
    function settleAudit($id, $audit = 1, $user = array(), $other = array()){
        $findData = $this->getData($id);
        $auditData = array(
            'settleAudit' =>  $audit==1 ? $findData['settleApply'] : 0,
            'settleAuditRs' => $audit,
            'settleAuditRole' => $user['userRole']
        );
        if ($other){
            $auditData = array_merge($auditData, $other);
        }
        return $this->db->update($this->table, $auditData,array('id'=>$id));
    }
}